#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Event.h>
#pragma unmanaged 
#include <ql\experimental\credit\defaultevent.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Experimental { namespace Credit {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDefaultEvent
	public ref class CDefaultEvent : 
            public CEvent,
            public Cephei::QL::Experimental::Credit::IDefaultEvent
	{
	protected: 
		boost::shared_ptr<QuantLib::DefaultEvent>* _ppDefaultEvent;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::DefaultEvent>* _phDefaultEvent;
#endif
		Object^ _DefaultEventOwner;     // reference to object that manages the storage for this object
	internal:
		CDefaultEvent (DateTime creditEventDate, Cephei::QL::Experimental::Credit::IDefaultType^ atomicEvType, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settleDate, Microsoft::FSharp::Core::FSharpOption<Double>^ recoveryRate);
        CDefaultEvent (boost::shared_ptr<QuantLib::DefaultEvent>& childNative, Object^ owner);
        CDefaultEvent (QuantLib::DefaultEvent& childNative, Object^ owner);
        CDefaultEvent (CDefaultEvent^ copy);
        CDefaultEvent (System::Type^ t);
#ifdef STRUCT
        CDefaultEvent (QuantLib::DefaultEvent childNative);
#endif       
#ifdef HANDLE
		CDefaultEvent (QuantLib::Handle<QuantLib::DefaultEvent>& childNative, Object^ owner);
		CDefaultEvent (QuantLib::Handle<QuantLib::DefaultEvent> childNative);
#endif
		virtual ~CDefaultEvent ();
		!CDefaultEvent ();

	internal:
		QuantLib::DefaultEvent& GetReference ();
		boost::shared_ptr<QuantLib::DefaultEvent>& GetShared ();
		QuantLib::DefaultEvent* GetPointer ();
        void SetDefaultEvent (boost::shared_ptr<QuantLib::DefaultEvent> native)
        {
            if (_ppDefaultEvent != NULL)
                delete _ppDefaultEvent;
            _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (native);
            SetEvent (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppDefaultEvent));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::DefaultEvent>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Cephei::QL::ICurrency^ Currency 
        {
		    virtual Cephei::QL::ICurrency^ get () ;
        }
        property DateTime Date 
        {
		    virtual DateTime get () ;
        }
        property Cephei::QL::Experimental::Credit::IDefaultType^ DefaultType 
        {
		    virtual Cephei::QL::Experimental::Credit::IDefaultType^ get () ;
        }
        property QL::Experimental::Credit::SeniorityEnum EventSeniority 
        {
		    virtual QL::Experimental::Credit::SeniorityEnum get () ;
        }
        property Boolean HasSettled 
        {
		    virtual Boolean get () ;
        }
        property Boolean IsDefault 
        {
		    virtual Boolean get () ;
        }
        property Boolean IsRestructuring 
        {
		    virtual Boolean get () ;
        }
		virtual Boolean MatchesDefaultKey (Cephei::QL::Experimental::Credit::IDefaultProbKey^ contractKey) ;
		virtual Boolean MatchesEventType (Cephei::QL::Experimental::Credit::IDefaultType^ contractEvType) ;
		virtual Double RecoveryRate (QL::Experimental::Credit::SeniorityEnum seniority) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IDefaultEvent Vector interface
	ref class CDefaultEventVector : public System::MarshalByRefObject,  
							    public INativeVector<IDefaultEvent^>,
							    public INativeVector<Generic::ICell<IDefaultEvent^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::DefaultEvent>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::DefaultEvent>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::DefaultEvent>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CDefaultEventVector ();
#ifdef STRUCT
		CDefaultEventVector (std::vector<QuantLib::DefaultEvent>& vec, Object^ owner);
		CDefaultEventVector (std::vector<QuantLib::DefaultEvent> vec);
#endif       
		CDefaultEventVector (std::vector<boost::shared_ptr<QuantLib::DefaultEvent>>& vec, Object^ owner);
#ifdef HANDLE
		CDefaultEventVector (std::vector<QuantLib::Handle<QuantLib::DefaultEvent>>& vec, Object^ owner);
#endif
		virtual ~CDefaultEventVector ();
		!CDefaultEventVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IDefaultEvent^ default[int] 
		{
			virtual IDefaultEvent^ get (int index) = INativeVector<IDefaultEvent^>::default::get;
			virtual void set (int index, IDefaultEvent^ value) = INativeVector<IDefaultEvent^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Experimental::Credit::IDefaultEvent^>^ CItem[int] 
		{
			virtual Generic::ICell<IDefaultEvent^>^ get (int index) = INativeVector<Generic::ICell<IDefaultEvent^>^>::default::get;
			virtual void set (int index, Generic::ICell<IDefaultEvent^>^ value) = INativeVector<Generic::ICell<IDefaultEvent^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::DefaultEvent>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::DefaultEvent>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::DefaultEvent>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CDefaultEvent_Factory : public System::MarshalByRefObject,  public IDefaultEvent_Factory
	{
	public:
        virtual IDefaultEvent^ Create (DateTime creditEventDate, Cephei::QL::Experimental::Credit::IDefaultType^ atomicEvType, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, Microsoft::FSharp::Core::FSharpOption<DateTime>^ settleDate, Microsoft::FSharp::Core::FSharpOption<Double>^ recoveryRate);
        /* <summary> Create a hybrid Vector of IDefaultEvent, with event notification of changes </summary> */
        virtual Cephei::IVector<IDefaultEvent^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IDefaultEvent, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDefaultEvent^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IDefaultEvent^>^ CreateVector(System::Collections::Generic::IEnumerable<IDefaultEvent^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IDefaultEvent^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IDefaultEvent^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Experimental*/ } /*Credit */}
